package dao.impl;

import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import dao.UserDao;
import model.User;

@Repository(value="userDao")//为啥必须在这声明，controller中才能使用
public class UserDaoImpl implements UserDao {

	@Autowired
	EntityManagerFactory entityManagerFactory;//依赖注入成功
	
	@Override
	public Object getUserById(Integer id) {
		System.out.println("Impl注入EMF#:"+entityManagerFactory);
		Object q = entityManagerFactory.createEntityManager().createNativeQuery("select * from user where id="+id,User.class).getSingleResult();//原生SQL返回Object[],而非实体对象，需要转换
		User user1 = entityManagerFactory.createEntityManager().createQuery("select u from User u where u.id=:id", User.class).setParameter("id", id).getSingleResult();
		System.out.println("user1#:"+user1);
		
		return q;
	}

}
